package com.yuju.controller.order;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.SettingsDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.Failure;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.log.service.IRoomLogService;
import com.yuju.order.enums.RoomOrderReturnCodeEnum;
import com.yuju.order.model.RoomOrderModel;
import com.yuju.order.service.IRoomOrderService;
import com.yuju.roomStatus.model.RoomTypeStatusModel;
import com.yuju.roomStatus.service.IRoomTypeStatusService;
import com.yuju.util.Utils;
import com.yuju.vo.order.RoomOrderVO;
/**
 * 
 * 类名：RoomOrderController  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月25日 下午5:53:27  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("roomOrder")
public class RoomOrderController extends BaseController<RoomOrderVO, RoomOrderModel> {
	@Autowired
	private IRoomOrderService roomOrderService;
	@Autowired
	private IRoomTypeStatusService roomTypeStatusService;
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject jsonObject)   {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		String groupFlag = jsonObject.getString("groupFlag");
		if(groupFlag != null){
			roomOrderVO.setHotelId(null);
		}
		Integer orderType = jsonObject.getInteger("orderType");
		Integer outDay = jsonObject.getInteger("outDay");
		Result result = new Result();
		PageModel pageModel = roomOrderService.listForSearch(convert(roomOrderVO),
				roomOrderVO.getPage(), roomOrderVO.getRows(),orderType,outDay);
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 新预离报表
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年1月31日 下午4:08:30 <br/>
	 */
	@RequestMapping("freshDueOut")
	@ResponseBody
	public Result freshDueOut(@RequestBody JSONObject jsonObject)   {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		Result result = roomOrderService.freshDueOut(convert(roomOrderVO));
		return result;
	}
	
	/**
	 * 订单查询
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月15日 下午7:21:40 <br/>
	 */
	@RequestMapping("listWithAccounts")
	@ResponseBody
	public Result listForSearch(@RequestBody JSONObject jsonObject)   {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Integer orderType = jsonObject.getInteger("orderType");
		Integer outDay = jsonObject.getInteger("outDay");
		Result result = new Result();
		PageModel pageModel = roomOrderService.listForSearchWithAccounts(convert(roomOrderVO),
				roomOrderVO.getPage(), roomOrderVO.getRows(),orderType,outDay);
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = roomOrderService.save(convert(roomOrderVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 新增同来宾客
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年1月5日 下午5:48:26 <br/>
	 */
	@RequestMapping("editForGuest")
	@ResponseBody
	public Result editForGuest(@RequestBody JSONObject jsonObject) {
		String empId = jsonObject.getString("empId");
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		 JSONArray arrays_GuestCustom = jsonObject.getJSONArray("listRoomOrderGuest");
	     String jsonText_GuestCustom = arrays_GuestCustom.toJSONString();
	     List<RoomOrderVO> listRoomOrderGuest = JSON.parseObject
	    		 (jsonText_GuestCustom, new TypeReference<ArrayList<RoomOrderVO>>() {
	     });
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		for(RoomOrderVO roomOrderGuestVO:listRoomOrderGuest){
			//Map<String, String> failForReserveRoomGuestVo = FieldValidator.validateAll(roomOrderGuestVO, "tableOrder", true);
			List<Failure> certificateCodeFailure = FieldValidator.validate(roomOrderGuestVO, "certificateCode");
			List<Failure> certificateTypeFailure = FieldValidator.validate(roomOrderGuestVO, "certificateType");
			List<Failure> nameFailure = FieldValidator.validate(roomOrderGuestVO, "name");
			Map<String, String> failForReserveRoomGuestVo = new HashMap<String, String>();
			if (certificateCodeFailure != null && !certificateCodeFailure.isEmpty()) {
				for (Failure f : certificateCodeFailure) {
					failForReserveRoomGuestVo.put(f.rule().getName(), f.message());
				} 
			}
			if (certificateTypeFailure != null && !certificateTypeFailure.isEmpty()) {
				for (Failure f : certificateTypeFailure) {
					failForReserveRoomGuestVo.put(f.rule().getName(), f.message());
				} 
			}
			if (nameFailure != null && !nameFailure.isEmpty()) {
				for (Failure f : nameFailure) {
					failForReserveRoomGuestVo.put(f.rule().getName(), f.message());
				} 
			}
			if (!failForReserveRoomGuestVo.isEmpty()) {
				return validateError(failForReserveRoomGuestVo);
			}
		}
		Result result = new Result();
		int rows = roomOrderService.editForGuest(roomOrderVO.getId(),convert(listRoomOrderGuest),empId);
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_ORDER_GUEST_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_ORDER_GUEST_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_ORDER_GUEST_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_ORDER_GUEST_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("edit")
	@ResponseBody
	public Result edit(@RequestBody JSONObject jsonObject) {
		String hotelId = jsonObject.getString("hotelId");
		String groupId = jsonObject.getString("groupId");
		String hotelCode = jsonObject.getString("hotelCode");
		String reserveId = jsonObject.getString("reserveId");
		/*-创建者 修改者存储生成*/
		String empId = jsonObject.getString("empId");
	      /*主页面table中订单信息，储存order和上面的公共信息*/
		RoomOrderVO roomOrder = jsonObject.getObject("roomOrder", RoomOrderVO.class);
		roomOrder.setHotelCode(hotelCode);
		roomOrder.setHotelId(hotelId);
		roomOrder.setGroupId(groupId);
		roomOrder.setReserveId(reserveId);
		roomOrder.setEmpId(empId);
		roomOrder.setCreateUserId(empId);
		roomOrder.setUpdateUserId(empId);
        /*  获取订单表bean 和 map 由于bean中数据不全，不能全接受*/
        JSONArray arrays = jsonObject.getJSONArray("listRoomOrder");
        String jsonText = arrays.toJSONString();
        List<RoomOrderVO> listRoomOrder = JSON.parseObject(jsonText, new TypeReference<ArrayList<RoomOrderVO>>() {
        });
		 JSONArray arrays_GuestCustom = jsonObject.getJSONArray("listRoomOrderGuest");
         String jsonText_GuestCustom = arrays_GuestCustom.toJSONString();
         List<RoomOrderVO> listRoomOrderGuest = JSON.parseObject
        		 (jsonText_GuestCustom, new TypeReference<ArrayList<RoomOrderVO>>() {
         });
         // 参数校验
         String validType = "formOrder";
        if (2 == roomOrder.getRentType()) {
        	validType = "apartment";
        }
 		Map<String, String> fail = FieldValidator.validateAll(roomOrder, validType, true);
 		if (!fail.isEmpty()) {
 			return validateError(fail);
 		}
     	for(RoomOrderVO roomOrderVo : listRoomOrder){
			Map<String, String> failForRoomOrderVo = FieldValidator.validateAll(roomOrderVo, "tableOrder", true);
			if (!failForRoomOrderVo.isEmpty()) {
				return validateError(failForRoomOrderVo);
			}
		}
     	for(RoomOrderVO roomOrderGuestVO:listRoomOrderGuest){
			//Map<String, String> failForReserveRoomGuestVo = FieldValidator.validateAll(roomOrderGuestVO, "tableOrder", true);
			List<Failure> certificateCodeFailure = FieldValidator.validate(roomOrderGuestVO, "certificateCode");
			List<Failure> certificateTypeFailure = FieldValidator.validate(roomOrderGuestVO, "certificateType");
			List<Failure> nameFailure = FieldValidator.validate(roomOrderGuestVO, "name");
			Map<String, String> failForReserveRoomGuestVo = new HashMap<String, String>();
			if (certificateCodeFailure != null && !certificateCodeFailure.isEmpty()) {
				for (Failure f : certificateCodeFailure) {
					failForReserveRoomGuestVo.put(f.rule().getName(), f.message());
				} 
			}
			if (certificateTypeFailure != null && !certificateTypeFailure.isEmpty()) {
				for (Failure f : certificateTypeFailure) {
					failForReserveRoomGuestVo.put(f.rule().getName(), f.message());
				} 
			}
			if (nameFailure != null && !nameFailure.isEmpty()) {
				for (Failure f : nameFailure) {
					failForReserveRoomGuestVo.put(f.rule().getName(), f.message());
				} 
			}
			if (!failForReserveRoomGuestVo.isEmpty()) {
				return validateError(failForReserveRoomGuestVo);
			}
		}
     	List<RoomOrderModel> listRoomOrderMOdel = convert(listRoomOrder); 
     	List<RoomOrderModel> listRoomOrderGuestMOdel = convert(listRoomOrderGuest); 
     	
		Result result = new Result();
		if (roomOrder.getCheckinTime() == null) {
			roomOrder.setCheckinTime(new Date());
		}
		List<String> rows = roomOrderService.saveOrder(convert(roomOrder),listRoomOrderMOdel,listRoomOrderGuestMOdel);
		if (rows.size() > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(rows);
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = roomOrderService.updateSelective(convert(roomOrderVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 更改房管状态
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年1月24日 下午6:18:08 <br/>
	 */
	@RequestMapping("updateRoomManageStatus")
	@ResponseBody
	public Result updateRoomManageStatus(@RequestBody JSONObject jsonObject) {
		String empId = jsonObject.getString("empId");
		Byte roomManageStatus = jsonObject.getByte("roomManageStatus");
		 JSONArray listRoomOrder = jsonObject.getJSONArray("listRoomOrder");
		 // 参数校验
		 Map<String, String> fail = new HashMap<>();
		 if(roomManageStatus==null){
			 fail.put("roomManageStatus", "不能为空");
			 return validateError(fail);
		 }
		 if (listRoomOrder == null) {
			 fail.put("listRoomOrder", "不能为空");
			 return validateError(fail);
		 }
	     String idStr = listRoomOrder.toJSONString();
	     List<RoomOrderModel> idList = JSON.parseObject
	    		 (idStr, new TypeReference<ArrayList<RoomOrderModel>>() {
	     });
		Result result = new Result();
		int rows = roomOrderService.updateRoomManageStatus(roomManageStatus,idList,empId);
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_MANAGER_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_MANAGER_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_MANAGER_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_MANAGER_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 更新订单基本信息
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年1月24日 下午5:54:03 <br/>
	 */
	@RequestMapping("updateSelectTive")
	@ResponseBody
	public Result updateSelectTive(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = roomOrderService.updateSelective(convert(roomOrderVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 修改价格
	 * 功能：<br/>
	 * @param id roomPrice
	 * @author sunyl
	 * @Date 2017年12月9日 下午4:31:16 <br/>
	 */
	@RequestMapping("updateForPrice")
	@ResponseBody
	public Result updateForPrice(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "upPrice", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		// 参数校验
		Map<String, String> failId = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!failId.isEmpty()) {
			return validateError(failId);
		}
		if(roomOrderVO.getRoomPrice()==null && roomOrderVO.getMonthPrice()==null){
			Map<String,String> msg   = new HashMap<>(); 
			msg.put("检验错误", "roomPrice和monthPrice不能都为空");
			return validateError(msg);
		}
		Result result = new Result();
		RoomOrderModel rows = roomOrderService.updateForRoomPrice(roomOrderVO.getId(),roomOrderVO.getRoomPrice(),roomOrderVO.getMonthPrice(),roomOrderVO.getReasonRemark());
		if (rows !=null) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_PRICE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_PRICE_SUCCESS.getDesc());
			result.setData(rows);
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_PRICE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_PRICE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 修改离期
	 * 功能：<br/>
	 * @param id CheckoutTime
	 * @author sunyl
	 * @Date 2017年12月9日 下午4:31:16 <br/>
	 */
	@RequestMapping("updateForCheckoutTime")
	@ResponseBody
	public Result updateForCheckoutTime(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "upCheckoutTime", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		// 参数校验
		Map<String, String> failId = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!failId.isEmpty()) {
			return validateError(failId);
		}
		Result result = new Result();
		RoomOrderModel rows = roomOrderService.updateForCheckoutTime(roomOrderVO.getId(),roomOrderVO.getCheckoutTime());
		if (rows !=null) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_CHECKOURT_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_CHECKOURT_SUCCESS.getDesc());
			result.setData(rows);
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_CHECKOURT_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_CHECKOURT_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 换房
	 * 功能：<br/>
	 *	@param roomId roomCode roomPrice id  oldRoomId roomTypeId roomTypeName
	 * @author sunyl
	 * @Date 2017年12月9日 下午4:31:16 <br/>
	 */
	@RequestMapping("updateForRoom")
	@ResponseBody
	public Result updateForRoom(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "upRoomAndPrice", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		// 参数校验
		Map<String, String> failId = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!failId.isEmpty()) {
			return validateError(failId);
		}
		// 参数校验
		Map<String, String> failRoom = FieldValidator.validateAll(roomOrderVO, "upRoom", true);
		if (!failRoom.isEmpty()) {
			return validateError(failRoom);
		}
		Result result = new Result();
		RoomOrderModel rows = roomOrderService.updateForRoom(convert(roomOrderVO));
		if (rows !=null) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_ROOM_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_ROOM_SUCCESS.getDesc());
			result.setData(rows);
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_ROOM_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_ROOM_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 订单所属
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年1月5日 上午11:35:24 <br/>
	 */
	@RequestMapping("updateForSource")
	@ResponseBody
	public Result updateForSource(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "upSource", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		// 参数校验
		Map<String, String> failId = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!failId.isEmpty()) {
			return validateError(failId);
		}
		Result result = new Result();
		RoomOrderModel rows = roomOrderService.updateForSource(convert(roomOrderVO));
		if (rows !=null) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_SUCCESS.getDesc());
			result.setData(rows);
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = roomOrderService.del(roomOrderVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_DEL_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_DEL_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_DEL_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		Result result = new Result();
		RoomOrderModel GroupModel = roomOrderService.find(convert(roomOrderVO));
		if (Utils.isNotNull(GroupModel)) {
			result.setData(GroupModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 退出联房
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月15日 下午3:03:54 <br/>
	 */
	@RequestMapping("quitOrderRela")
	@ResponseBody
	public Result quitOrderRela(@RequestBody JSONObject jsonObject) {
		/*校验参数*/
        Map<String,String> fail = new HashMap<>();
		JSONArray arrays = jsonObject.getJSONArray("roomOrderIdList");
		if(null == arrays){
			fail.put("roomOrderIdList", "不能为空");
        	return validateError(fail);
		}
		String relaId = jsonObject.getString("relaId");
		if(relaId == null){
			fail.put("relaId", "不能为空");
			return validateError(fail);
		}
        String jsonText = arrays.toJSONString();
        List<String> roomOrderIdList = JSON.parseObject(jsonText, new TypeReference<ArrayList<String>>() {
        });
        if(roomOrderIdList==null ||roomOrderIdList.size() == 0){
        	fail.put("roomOrderIdList", "不能为空");
        	return validateError(fail);
        }
		Result result = new Result();
		int rows = roomOrderService.quitOrderRela(roomOrderIdList,relaId);
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_QUIT_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_QUIT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_QUIT_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_QUIT_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 加入联房
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月15日 下午3:07:50 <br/>
	 */
	@RequestMapping("joinOrderRela")
	@ResponseBody
	public Result joinOrderRela(@RequestBody JSONObject jsonObject) {
		/*校验参数*/
        Map<String,String> fail = new HashMap<>();
        String groupId = jsonObject.getString("groupId");
        String hotelId = jsonObject.getString("hotelId");
        String roomOrderId = jsonObject.getString("roomOrderId");
		JSONArray arrays = jsonObject.getJSONArray("roomOrderIdList");
		if(null == arrays){
			fail.put("roomOrderIdList", "不能为空");
        	return validateError(fail);
		}
		if(roomOrderId == null){
			fail.put("roomOrderId", "不能为空");
			return validateError(fail);
		}
        String jsonText = arrays.toJSONString();
        List<String> roomOrderIdList = JSON.parseObject(jsonText, new TypeReference<ArrayList<String>>() {
        });
        if(roomOrderIdList == null ||roomOrderIdList.size() == 0){
        	fail.put("roomOrderIdList", "不能为空");
        	return validateError(fail);
        }
		Result result = new Result();
		int rows = roomOrderService.joinOrderRela(roomOrderId,roomOrderIdList,groupId,hotelId);
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_JOIN_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_JOIN_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_JOIN_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_JOIN_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 解散联房
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月15日 下午3:07:50 <br/>
	 */
	@RequestMapping("dissolveOrderRela")
	@ResponseBody
	public Result dissolveOrderRela(@RequestBody JSONObject jsonObject) {
		/*校验参数*/
		Map<String,String> fail = new HashMap<>();
		String relaId = jsonObject.getString("relaId");
		if(relaId == null){
			fail.put("relaId", "不能为空");
			return validateError(fail);
		}
		Result result = new Result();
		List<RoomOrderModel> roomOrderList = roomOrderService.dissolveOrderRela(relaId);
		if (roomOrderList!=null && roomOrderList.size()>0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_DISSOLVE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_DISSOLVE_SUCCESS.getDesc());
			result.setData(roomOrderList);
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_DISSOLVE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_ROOM_DISSOLVE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 获取关联订单
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月9日 下午4:59:34 <br/>
	 */
	@RequestMapping("getOrderRela")
	@ResponseBody
	public Result getOrderRela(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		Result result = new Result();
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		List<RoomOrderModel> finds = roomOrderService.getOrderRela(roomOrderVO.getId());
		if (Utils.isNotNull(finds)) {
			result.setData(finds);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 获取关联订单，详细，包含生成的关联订单
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月19日 上午9:53:45 <br/>
	 */
	@RequestMapping("getOrderRelaByOrderId")
	@ResponseBody
	public Result getOrderRelaByOrderId(@RequestBody JSONObject jsonObject) {
		RoomOrderVO roomOrderVO = jsonObject.toJavaObject(RoomOrderVO.class);
		Result result = new Result();
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		List<RoomOrderModel> finds = roomOrderService.getOrderRelaByOrderId(roomOrderVO.getId());
		if (Utils.isNotNull(finds)) {
			result.setData(finds);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：<br/>
	 * 计算公寓房日均价和总房费<br/>
	 * @author sunyl
	 * @Date 2017年12月19日 上午9:53:45 <br/>
	 */
	//@RequestMapping("getAveragePirceAndTotalPriceByMonthPrice")
	@RequestMapping("getApartPrice")
	@ResponseBody
	public Result getApartPrice(@RequestBody JSONObject jsonObject) {
		Result result = new Result();
		// 参数校验
		BigDecimal monthPrice = jsonObject.getBigDecimal("monthPrice");
		Date startDay = jsonObject.getDate("startDay");
		Date endDay = jsonObject.getDate("endDay");
		String  reasonRemark = jsonObject.getString("reasonRemark");
		Map<String, String> fail = new HashMap<>();
		if(monthPrice == null || startDay == null || endDay == null){
			fail.put("monthPrice  startDay  endDay ", "都不能为空");
			return validateError(fail);
		}
		RoomTypeStatusModel roomTypeStatusModel = roomTypeStatusService.getApartPrice(startDay, endDay, monthPrice);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("averagePirce",roomTypeStatusModel.getDayPrice());
		resultMap.put("totalPrice", roomTypeStatusModel.getTotalPrice());
		resultMap.put("monthPrice", monthPrice);
		resultMap.put("reasonRemark", reasonRemark);
		if (Utils.isNotNull(resultMap)) {
			result.setData(resultMap);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderReturnCodeEnum.ROOM_ORDER_AVERAGE_AND_ALL_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderReturnCodeEnum.ROOM_ORDER_AVERAGE_AND_ALL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
}
